package wxb.avalon.core.controller;


import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import wxb.avalon.core.entity.Room;
import wxb.avalon.core.entity.dto.RoomPlayerDto;
import wxb.avalon.core.entity.vo.RoomRoleVo;
import wxb.avalon.core.service.IRoomService;
import wxb.avalon.core.bean.Result;
import wxb.avalon.core.util.RHUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxb
 * @since 2022-01-11
 */
@Controller
@RequestMapping("/core/room")
public class RoomController {

    @Autowired
    private IRoomService roomService;


    @GetMapping("/test")
    @ResponseBody
    public Result<Object> test(HttpServletRequest req){
        Result<Object> result = new Result<>();
        try {
            Integer playerId = RHUtil.getPlayerId(req);
            if (null == playerId){
                return result.fail("请先登录");
            }
            return result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return result.fail("系统异常："+e.getLocalizedMessage());
        }
    }

    @GetMapping("/get/{id}")
    @ResponseBody
    public Result<Room> get(HttpServletRequest req, @PathVariable Integer id){
        Result<Room> result = new Result<>();
        try {
            Integer playerId = RHUtil.getPlayerId(req);
            if (null == playerId){
                return result.fail("请先登录");
            }
            return result.ok(roomService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return result.fail("系统异常："+e.getLocalizedMessage());
        }
    }


    @GetMapping("/join")
    @ResponseBody
    public Result<List<RoomPlayerDto>> join(@RequestParam(name = "roomNo") String roomNo,
                     @RequestParam(name = "debug", required = false, defaultValue = "0") Integer debug,
                                             HttpServletRequest req){
        Result<List<RoomPlayerDto>> result = new Result<>();
        try {
            Integer playerId;
            if (debug == 1){
                playerId = RandomUtil.randomInt();
            }else {
                playerId = RHUtil.getPlayerId(req);
                if (null == playerId){
                    return result.fail("请先登录");
                }
            }

            return roomService.join(roomNo, playerId);
        }catch (Exception e){
            e.printStackTrace();
            return result.fail("系统异常："+e.getLocalizedMessage());
        }

    }

    @RequestMapping("/create")
    @ResponseBody
    public Result<Room> create(@RequestBody RoomRoleVo vo, HttpServletRequest req){
        Result<Room> result = new Result<>();
        try {
            Integer playerId = RHUtil.getPlayerId(req);
            if (null == playerId){
                return result.fail("请先登录");
            }
            Room room = vo.getRoom();
            room.setCreatePlayerId(playerId);
            room = roomService.create(vo);
            return result.ok(room);
        }catch (Exception e){
            e.printStackTrace();
            return result.fail("系统异常："+e.getLocalizedMessage());
        }
    }

}
